<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属上级" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="所属上级"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="类型(0:目录,1:菜单,2:按钮)" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型(0:目录,1:菜单,2:按钮)"></el-input>
    </el-form-item>
    <el-form-item label="路由地址" prop="path">
      <el-input v-model="dataForm.path" placeholder="路由地址"></el-input>
    </el-form-item>
    <el-form-item label="组件路径" prop="component">
      <el-input v-model="dataForm.component" placeholder="组件路径"></el-input>
    </el-form-item>
    <el-form-item label="权限标识" prop="perms">
      <el-input v-model="dataForm.perms" placeholder="权限标识"></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="图标"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sortValue">
      <el-input v-model="dataForm.sortValue" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="高亮的 path" prop="activeMenu">
      <el-input v-model="dataForm.activeMenu" placeholder="高亮的 path"></el-input>
    </el-form-item>
    <el-form-item label="" prop="isHide">
      <el-input v-model="dataForm.isHide" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="状态(0:禁止,1:正常)" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态(0:禁止,1:正常)"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标记（0:不可用 1:可用）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除标记（0:不可用 1:可用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          name: '',
          type: '',
          path: '',
          component: '',
          perms: '',
          icon: '',
          sortValue: '',
          activeMenu: '',
          isHide: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '所属上级不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型(0:目录,1:菜单,2:按钮)不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '路由地址不能为空', trigger: 'blur' }
          ],
          component: [
            { required: true, message: '组件路径不能为空', trigger: 'blur' }
          ],
          perms: [
            { required: true, message: '权限标识不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ],
          sortValue: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          activeMenu: [
            { required: true, message: '高亮的 path不能为空', trigger: 'blur' }
          ],
          isHide: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(0:禁止,1:正常)不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除标记（0:不可用 1:可用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/system/sysmenu/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.parentId = data.sysMenu.parentId
                this.dataForm.name = data.sysMenu.name
                this.dataForm.type = data.sysMenu.type
                this.dataForm.path = data.sysMenu.path
                this.dataForm.component = data.sysMenu.component
                this.dataForm.perms = data.sysMenu.perms
                this.dataForm.icon = data.sysMenu.icon
                this.dataForm.sortValue = data.sysMenu.sortValue
                this.dataForm.activeMenu = data.sysMenu.activeMenu
                this.dataForm.isHide = data.sysMenu.isHide
                this.dataForm.status = data.sysMenu.status
                this.dataForm.createTime = data.sysMenu.createTime
                this.dataForm.updateTime = data.sysMenu.updateTime
                this.dataForm.isDeleted = data.sysMenu.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/sysmenu/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'path': this.dataForm.path,
                'component': this.dataForm.component,
                'perms': this.dataForm.perms,
                'icon': this.dataForm.icon,
                'sortValue': this.dataForm.sortValue,
                'activeMenu': this.dataForm.activeMenu,
                'isHide': this.dataForm.isHide,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            }).catch(e=>{
              this.visible = false
              this.$emit('refreshDataList')

              console.log(e);
              
            })
          }
        })
      }
    }
  }
</script>
